package cn.ch.train.business.service;

import cn.ch.train.business.domain.Train;
import cn.ch.train.business.domain.TrainExample;
import cn.ch.train.business.enums.BusinessExceptionEnum;
import cn.ch.train.business.mapper.TrainMapper;
import cn.ch.train.business.request.TrainQueryParam;
import cn.ch.train.business.request.TrainSaveParam;
import cn.ch.train.business.response.TrainQueryResult;
import cn.ch.train.common.exception.BusinessException;
import cn.ch.train.common.response.PageResult;
import cn.ch.train.common.util.SnowUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class TrainService {
    private static final Logger logger = LoggerFactory.getLogger(TrainService.class);
    @Resource
    private TrainMapper trainMapper;

    public void save(TrainSaveParam param) {
        DateTime now = DateTime.now();
        Train train = BeanUtil.copyProperties(param, Train.class);
        // 保存之前，先校验唯一键是否存在
        Train trainDb = selectByUnique(param.getCode());
        if (Objects.nonNull(trainDb)) {
            throw new BusinessException(BusinessExceptionEnum.TRAIN_HAS_EXIST.getDesc());
        }
        if (Objects.isNull(train.getId())) {
            train.setId(SnowUtil.getSnowFlakeNextId());
            train.setCreateTime(now);
            train.setUpdateTime(now);
            trainMapper.insert(train);
        } else {
            train.setUpdateTime(now);
            trainMapper.updateByPrimaryKey(train);
        }
    }

    private Train selectByUnique(String code) {
        TrainExample stationExample = new TrainExample();
        TrainExample.Criteria criteria = stationExample.createCriteria();
        criteria.andCodeEqualTo(code);
        List<Train> stationList = trainMapper.selectByExample(stationExample);
        if (CollUtil.isNotEmpty(stationList)) {
            return stationList.get(0);
        }
        return null;
    }

    public PageResult<TrainQueryResult> queryList(TrainQueryParam param) {
        TrainExample trainExample = new TrainExample();
        trainExample.setOrderByClause("code asc");
        TrainExample.Criteria criteria = trainExample.createCriteria();
        PageHelper.startPage(param.getPageNo(), param.getPageSize());
        List<Train> trainList = trainMapper.selectByExample(trainExample);

        PageInfo<Train> trainPageInfo = new PageInfo<>(trainList);
        logger.info("总行数：{}", trainPageInfo.getTotal());
        logger.info("总页数：{}", trainPageInfo.getPages());
        List<TrainQueryResult> trainQueryList = BeanUtil.copyToList(trainList, TrainQueryResult.class);
        PageResult<TrainQueryResult> pageResult = new PageResult<>();
        pageResult.setTotal(trainPageInfo.getTotal());
        pageResult.setList(trainQueryList);
        return pageResult;
    }

    public void delete(Long id) {
        trainMapper.deleteByPrimaryKey(id);
    }

    public List<TrainQueryResult> queryAll() {
        TrainExample trainExample = new TrainExample();
        trainExample.setOrderByClause("code asc");
        List<Train> trainList = trainMapper.selectByExample(trainExample);

        List<TrainQueryResult> trainQueryList = BeanUtil.copyToList(trainList, TrainQueryResult.class);
        return trainQueryList;
    }
}
